let storage = bui.storage();
window.router = bui.router();
// console.log(window.location.href)
bui.ready(function (global) {

    let languageArr = [
        {
            id: 'russian',
            name: 'Русский язык'
        },
        {
            id: 'indonesian',
            name: 'bahasa indonesia'
        },
        {
            id: 'arabic',
            name: 'بالعربية'
        },
        {
            id: 'korean',
            name: '한국어'
        },
        {
            id: 'czech',
            name: 'Čeština'
        },
        {
            id: 'vietnamese',
            name: 'Tiếng Việt'
        }
    ]
    storage.set('languageArr', languageArr)
    // const vConsole = new VConsole()
    // 初始化路由
    router.init({
        id: "#bui-router",
        progress: false,
        firstAnimate:false,
        hash: true,
        cache: true,
        syncHistory: true,
        effect: 'fadein',
        backEffect: 'none',
        indexModule: {
            template: "pages/mainIndex/mainIndex.html",
            script: "pages/mainIndex/mainIndex.js",
        },
        // indexModule: {
        //     template: "pages/main/main.html",
        //     script: "pages/main/main.js",
        // },
        beforeLoad(e) {
            const url = window.location.href

            // console.log("url:",url)
            if (url.indexOf("?") > -1 && url.indexOf("#") < 0) {
                let param = url.split("?")[1]
                let codeP = param.split("&")[0]
                let code = codeP.split("=")[1]
                let stateP = param.split("&")[1]
                let state = stateP.split("=")[1]
                // console.log("code:", code)
                // console.log("state:", state)
                // console.log("isWeiXin:", isWeiXin())
                if (state) {
                    if (isWeiXin()) {
                        // 需要传入code才能获取到用户的信息
                        wxScope(code, global.api.gzhLogin, state)
                    } else {
                        // 这是我调用的后台接口，详见1.2
                        // 需要传入code才能获取到用户的信息
                        wxScope(code, global.api.htmlLogin, state)
                    }
                } else {
                    if (code) {
                        if (isWeiXin()) {
                            // 这是我调用的后台接口，详见1.2
                            // 需要传入code才能获取到用户的信息
                            wxScope(code, global.api.gzhLogin, null)
                        } else {
                            // 这是我调用的后台接口，详见1.2
                            // 需要传入code才能获取到用户的信息
                            wxScope(code, global.api.htmlLogin, null)
                        }
                    }
                }
            }
        }
    })


    // 绑定事件
    // bind();

})

// 事件类定义
function bind() {
    // 绑定页面的所有按钮有href跳转
    bui.btn({ id: "#bui-router", handle: ".bui-btn" }).load();

    // // 统一绑定页面所有的后退按钮
    // router.$("#bui-router").on("click", ".btn-back", function (e) {
    //     let id = $(e.currentTarget).attr("id")
    //     if (id != "articleFh" && id != "fhZjFh" && id != "articleEditFh" && id != "articleAudit" && id != "commentFh" && id != "userManagerFh" && id != "advertiseFh" && id != "footerFh" && id != "typeFh") {
    //         // 支持后退多层,支持回调
    //         bui.back();
    //     }
    // })
}

function isWeiXin() {
    let ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true
    } else {
        return false
    }
}

function wxScope(code, url, state) {
    console.log("登录url:", url)
    console.log("传递state:", state)
    bui.ajax({
        url: url,
        data: {code: code, state: state},
        method: "POST"
    }).then(function(res){
        console.log(res)
        let data = res.data
        if (res.code == "200") {
            storage.set("token", res.token)
            storage.set("user", data)

            if (res.msg == '绑定成功') {
                storage.set("bindErrMsg", res.msg)
            }

            let dlTz = storage.get("dlTz", 0)
            if (dlTz == '0') {
                router.replace({ url: "pages/fbZj/fbZj.html"});
            } else if (dlTz == '1') {
                router.replace({ url: "pages/articleInfo/articleInfo.html"});
            }else if (dlTz == '2') {
                storage.set("tzPage", '2')
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            }else if (dlTz == '9') {
                router.replace({ url: "pages/fbZjGzh/fbZjGzh.html"});
            }else if (dlTz == '10') {
                router.replace({ url: "pages/articleEdit/articleEdit.html"});
            }else if (dlTz == '11') {
                router.replace({ url: "pages/articleList/articleList.html"});
            }else if (dlTz == '12') {
                router.replace({ url: "pages/commentList/commentList.html"});
            }else if (dlTz == '13') {
                router.replace({ url: "pages/userManager/userManager.html"});
            }else if (dlTz == '14') {
                router.replace({ url: "pages/endEdit/endEdit.html"});
            }else if (dlTz == '16') {
                router.replace({ url: "pages/advertiseList/advertiseList.html"});
            }else if (dlTz == '15') {
                router.replace({ url: "pages/audit/audit.html"});
            }else if (dlTz == '17') {
                router.replace({ url: "pages/auditComment/auditComment.html"});
            }else if (dlTz == '18') {
                router.replace({ url: "pages/dashboard/dashboard.html"});
            }else {
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            }
        } else {
            if (state) {
                if (!res.msg) {
                    res.msg = '绑定失败！'
                }
                storage.set("bindErrMsg", res.msg)
                storage.set("tzPage", '2')
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            } else {
                router.replace({ url: "pages/error/error.html", param: {msg: "登录失败"}});
            }
        }
    })
}
